<?php
/**
 * JoiRights - библиотека для работы с правами пользователей
 *
 * Core Class
 *
 * @version 1.0 
 * @package JoiRights
 * @filename joiRights.class.php
 * @author JoostinaTeam
 * @copyright (C) 2008-2009 Joostina Team
 * @license GNU GPL 2.0. Пожалуйста, не удаляйте копирайты из исходного кода. Спасибо. 
 *
 **/
 DEFINE('JOIRIGHTS', '1');
 class joiRights{
	
	var $_me = null;
	var $_error = null;
	var $_rights_label = null;
	var $_error_messages = null;
	
	function joiRights($config_rights, $rights_label, $error_messages = array()){
		
		$this->_rights_label = $rights_label;
		$this->_error_messages = $error_messages;
		
		$this->bind_rights($config_rights);		
	}
	
	function bind_rights($config_rights){
		
		$rights = array();
		$rights0 = explode('*', $config_rights);
		
		foreach($rights0 as $right){
			$arr = explode('=', $right);
			if(isset($arr[1])){
				$rights[$arr[0]] = explode( ',' , $arr[1] );
			}

		}
		
		$rows = get_object_vars($this);
		
		foreach ($rows as $key => $value) {
			if(substr($key, 0, 1) !== '_'){
				if (isset($rights[$key])){
					$this->$key = $rights[$key];
				}
				else{
					$this->$key = array();	
				}	
				
			}
		}
					
	}
	
	function prepare_for_saving($array){		
		$return = '';
				
		foreach($array as $key => $v){
			$return .= $key.'='.implode(',' , $v).'*';	
		}		
		return $return;		
	}
	
	function allow_me($action, $gid){
	
		if(isset($this->$action) && is_array($this->$action) && in_array($gid, $this->$action)){
			return true;
		}
		else{
			return false;
		}	
	}
	
	function draw_config_table(){
		
		$rights = get_object_vars($this);			
		$groups = $this->get_all_user_groups();		
				
		//Шапка таблицы
		$return = '<table class="adminlist" width="100%"><tr><th><span id="check-all"><a class="checker active" id="check_it" href="#">Отметить все</a><a class="checker" id="uncheck_it" href="#">Снять отметки</a></span></th>';
		foreach($groups as $key => $v){
			$return .='<th>'.$v.'</th>';
		}		
		$return .= '</tr>';
		
		
		//Таблица с чекбоксами	
		foreach($rights as $right => $allow_groups){
			
			if(substr($right, 0, 1) !== '_'){				
			
				$return .='<tr>';			
				$return .='<td>'.$this->get_label($right).'</td>';
				
				$i = 0;
				foreach($groups as $gid => $gname){
					$checked = (in_array($gid, $allow_groups) ? 'checked="checked"' : '');
					$return .= '<td><input type="checkbox" class="urights_box" name="u_rights['.$right.']['.$i.']" value="'.$gid.'" '.$checked.' /></td>';
					$i++;
				}
				
				$return .='</tr>';	
			}
		}		
		$return .= '</table>';
		
		return $return;
	}

	
	function get_all_user_groups(){
		
		$groups = array(
			 0 => 'Guest',
			18 => 'Registered',
			19 => 'Author',
			20 => 'Editor',
			21 => 'Publisher',
			23 => 'Manager',
			24 => 'Administrator',
			25 => 'Super Administrator'
		);
		
		return $groups;	
	}
	
	
	function get_label($right){
		
		if(isset($this->_rights_label[$right])){
			return $this->_rights_label[$right];	
		}
		else{
			return $right;	
		}
				
	}
	
	function error($right = ''){
		if($right && isset($this->_error_messages[$right])){
			return $this->_error_messages[$right];	
		}
		else{
			return 'Accsess denided';	
		}		
	}
}